<html>
<head>
	<meta charset='utf-8' />
	<title>小区信息助手后台</title>
	<meta name="viewport" content="width=device-width,initial-scale=1.0,maximum-scale=1.0,user-scalable=0" />
	<style>
		@media screen and (max-width:450px){
			#content,.nav_div{width:98%;margin:0 auto}
			/*页面宽度900以下时 .abc的宽度为450*/
		}
		@media screen and (min-width:450px){
			#content,.nav_div{width:450px;margin:0 auto}
			/*页面宽度900以下时 .abc的宽度为450*/
		}
	</style>
</head>
<body>
<div id='content' style='position:relative;border:0px solid'>
	<h1 style='text-align:center'> 管理权限设置</h1>

	<div style='text-align:center;font-size:12px'>
		当前用户：<span id='current_user'></span>
	</div>


	<!--城市选择列表-->
	<div id='city_list'>
		<div style='text-indent:10px;margin:10px;position:relative'>
			<label><input type="checkbox" /> 吉林省</label>　<button style='position:absolute;right:0px'>收/展</button>
			<div style='text-indent:90px;margin:10px 0;position:relative'>
				<label><input type="checkbox" /> 通化市</label>　<button style='position:absolute;right:0px'>收/展</button>
				<div style='text-indent:170px;margin:10px'>
					<label><input type="checkbox" /> 柳河县</label>
				</div>
				<div style='text-indent:170px;margin:10px'>
					<label><input type="checkbox" /> 柳河县</label>
				</div>
			</div>
		</div>
	</div>
	
	<div style='text-align:center;margin:50px 0'>
		<button id='submit'>提交</button>
		<button onclick="location.replace('after_con_admin.html')">返回</button
	</div>

</div>



</body>
</html>
<script src='../public/jquery.js'></script>
<script src='../public/fun.js'></script>
<script>
	//提交设置的权限
	function perm_submit(){
		$('#submit').click(function(){
			var id_list = [];
			$('input[type=checkbox').each(function(i,o){
				if(o.checked){
					id_list.push(o.value);
				}
			});
			Ajax2({type:'set_admin_perm',user_id:sessionStorage.current_admin_id,city_id_list:JSON.stringify(id_list)},function(res){
				if(res == 'ok'){
					A('操作成功！');
					location.reload();
				}else{
					A('操作异常！');
				}
			});
		})
	}
$(function(){



	//底部导航与显示页面
	$.ajax({
		url:'after_template_bottom_nav.html',
		success(res){
			$('body').append(res);
		}
	});

	//判断用户名和id缓存 ，并显示用户名
	if(sessionStorage.currnet_admin_name && sessionStorage.current_admin_id){
		$('#current_user').text(sessionStorage.currnet_admin_name);
	}else{
		location.replace('after_con_admin.html');
	}


	//获取开通城市数据
	var view1 = 0;
	if(sessionStorage.open_city){
		if(view1)C('sess');
		parse_city_data(sessionStorage.open_city);
	}else{
		Ajax({},function(res){
			if(/^\[.*\]$/.test(res)){
				sessionStorage.open_city = res;
				if(view1)C('upyun');
				parse_city_data(res);	//解析城市数据
			}else{
				Ajax2({type:'get_open_city_list'},function(res2){
					if(view1)C('after');
					parse_city_data(res2);	//解析城市数据
				})
			}

		},upyun_url + '/open_city/open_city.html',0,1);
	}


});

//获取用户对县权限id列表
function get_admin_user_perm_list(fun){
	Ajax2({type:'get_admin_user_perm_list',user_id:sessionStorage.current_admin_id},function(res){
		fun(res)
	});
}


//解析城市数据
function parse_city_data(res){

	get_admin_user_perm_list(function(perm_id_list){		//获取用户对县权限id列表
		var perm_id_list = JSON.parse(perm_id_list);
		var city_str = '';
		if(/^\[.*\]$/.test(res)){
			var citys = JSON.parse(res);
			for(var i=0;i<citys.length;i++){
				city_str += "<div style='text-indent:10px;margin:10px;position:relative'>";
					city_str += "<label>" + citys[i].name + "</label>　<button style='position:absolute;right:0px' onclick=v2d(this)>收/展</button>";

					city_str += "<div style='display:nones'>";
					for(var j=0;j<citys[i].city.length;j++){
						city_str += "<div style='text-indent:90px;margin:10px 0;position:relative'>";
							city_str += "<label>"+ citys[i].city[j].name +"</label>　<button style='position:absolute;right:0px' onclick=v2d(this)>收/展</button>";
							city_str += "<div style='display:nones'>";
							for(var x=0;x<citys[i].city[j].county.length;x++){
								city_str += "<div style='text-indent:170px;margin:10px'>";
									var tmp_stat = '';
									for(var y=0;y<perm_id_list.length;y++){
										if(perm_id_list[y] == citys[i].city[j].county[x].id){
											tmp_stat = 'checked';
										}
									}
									city_str += "<label><input type='checkbox' "+tmp_stat+" value='"+citys[i].city[j].county[x].id+"' />"+ citys[i].city[j].county[x].name +"</label>";
								city_str += "</div>";
							}
							city_str += "</div>";
						city_str += "</div>";
					}
					city_str += "</div>";

				city_str += "</div>";
			}
		}
		$('#city_list').html(city_str);


		perm_submit();
	});


}

function v2d(o){
	if($(o).next('div').is(':hidden')){
		$(o).next('div').show();
	}else{
		$(o).next('div').hide();
	}
}
</script>